package hik.utils;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import hik.entity.preset.ThermometryRegion;
import hik.entity.preset.ThermometryScene;

public class JaxbUtil {

	/**
	 * JavaBean装换成xml 默认编码UTF-8
	 * 
	 * @param obj
	 * @return
	 */
	public static String converTomXml(Object obj) {
		return converToXml(obj, "UTF-8");

	}

	/**
	 * JavaBean装换成xml
	 * 
	 * @param obj
	 * @param encoding
	 * @return
	 */
	private static String converToXml(Object obj, String encoding) {
		String result = null;
		try {
			JAXBContext context = JAXBContext.newInstance(obj.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
			StringWriter writer = new StringWriter();
			marshaller.marshal(obj, writer);
			result = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * xml装换成JavaBean
	 * 
	 * @param xml
	 * @param c
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T converyToJavaBean(String xml, Class<T> c) {
		T t = null;
		try {
			JAXBContext context = JAXBContext.newInstance(c);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			t = (T) unmarshaller.unmarshal(new StringReader(xml));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	
	public static void main(String[] args) {
		ThermometryScene thermometryRegion = new ThermometryScene(1);
		String str = JaxbUtil.converTomXml(thermometryRegion);
		Pattern p = Pattern.compile("thermometryScene");
        Matcher m = p.matcher(str);
        str = m.replaceAll("ThermometryScene");
		System.out.println(str);
	}
	
	/**
	 * @throws JAXBException
	 */
	public static void showMarshaller() {
		ThermometryRegion thermometryRegion = new ThermometryRegion(1);
		String str = JaxbUtil.converTomXml(thermometryRegion);
		System.out.println(str);

	}

	/**
	 * 转换 hik 设置预置位信息 为xml
	 * @param thermometryRegion
	 * @return
	 */
	public static String converTomXmlString(ThermometryRegion region) {
		String str = JaxbUtil.converTomXml(region);
		Pattern p = Pattern.compile("thermometryRegion");
        Matcher m = p.matcher(str);
        str = m.replaceAll("ThermometryRegion");
		return str;
	}

}